package com.ms.education.ucenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ms.education.R;
import com.ms.education.ucenter.entity.Teacher;
import com.ms.education.ucenter.mapper.TeacherMapper;
import com.ms.education.ucenter.service.TeacherService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 课程的教师 服务实现类
 * </p>
 *
 * @author ms
 * @since 2023-01-12
 */
@Service
public class TeacherServiceImp extends ServiceImpl<TeacherMapper, Teacher> implements TeacherService {

    @Override
    public R pageTeacher(Teacher teacher, long current, long limit) {
        Page<Teacher> page = new Page<>(current, limit);
        QueryWrapper<Teacher> wrapper = new QueryWrapper<>();
        String name = teacher.getName();
        String career = teacher.getCareer();//资历
        Integer level = teacher.getLevel();//1:高级讲师 2:首席讲师
        Date beginTime = teacher.getBeginTime();
        Date endTime = teacher.getEndTime();
        Boolean priority = teacher.getPriority();
        if (!StringUtils.isEmpty(name)) {
            wrapper.like("name", name);
        }
        if (!StringUtils.isEmpty(career)) {
            wrapper.like("career", career);
        }
        if (!StringUtils.isEmpty(level)) {
            wrapper.eq("level", level);
        }
        if (!StringUtils.isEmpty(beginTime)) {
            wrapper.ge("gmt_create", beginTime);
        }
        if (!StringUtils.isEmpty(endTime)) {
            wrapper.le("gmt_create", endTime);
        }
        if (!StringUtils.isEmpty(priority)) {
            if (priority) {
                wrapper.orderByAsc("sort");
            } else {
                wrapper.orderByDesc("sort");
            }
        }
        wrapper.orderByDesc("gmt_modified");
        this.page(page, wrapper);
        long rows = page.getTotal();
        List<Teacher> teachers = page.getRecords();

        return R.ok().data("rows", rows).data("teachers", teachers);
    }

    @Override
    public List<Teacher> index() {

        QueryWrapper<Teacher> wrapper = new QueryWrapper<>();
        wrapper.orderByAsc("sort").last("limit 4");
        return list(wrapper);
    }
}
